﻿using System;
using System.Composition.Hosting;
using System.Reflection;

namespace Padsurface.App
{
    public static class CompositionContainer
    {
        private static CompositionHost _compositionHost;
        private static CompositionHost CompositionHost
        {
            get
            {
                if (_compositionHost == null)
                    throw new InvalidOperationException("Call Initialize first, before using CompositionContainer");

                return _compositionHost;
            }
            set
            {
                _compositionHost = value;
            }
        }

        public static void Initialize()
        {
            var configuration = new ContainerConfiguration()
                .WithAssembly(typeof(Padsurface.App.App).GetTypeInfo().Assembly)
                .WithAssembly(typeof(Padsurface.CoreAudio.Wrappers.Services.Interfaces.IAudioClientActivator).GetTypeInfo().Assembly)
                .WithAssembly(typeof(Padsurface.Audio.Wave.AudioEngine).GetTypeInfo().Assembly);

            CompositionHost = configuration.CreateContainer();
        }

        public static T GetInstance<T>()
        {
            return CompositionHost.GetExport<T>();
        }
    }
}